Python Programming Lab · Exp-10(c)

Data Visualization with Matplotlib

A visual tour of Matplotlib.Pyplot, drawing plots, customizing aesthetics, and working with complex charts.

📈 Line Plots 📊 Bar Charts 🎯 Scatter Plots

Use ← → keys or swipe to navigate

Session Overview

What We Cover Today

01

Introduction to Pyplot

Importing, basic syntax, and showing plots.

02

Aesthetics & Styling

Controlling markers, lines, labels, and grids.

03

Types of Charts

Scatter, Bar, Histogram, and Pie charts.

04

Subplots

Combining multiple plots into one figure.

Introduction

What is Matplotlib?

Matplotlib is the most popular data visualization library for Python. It allows you to create static, animated, and interactive visualizations with just a few lines of code.

Pyplot (matplotlib.pyplot)

Most of the Matplotlib utilities lie under the pyplot submodule, acting almost identically to MATLAB's plotting functions.

# Standard import convention
import matplotlib.pyplot as plt
import numpy as np
  • Interactive and Expressive: Allows fine-tuning of every aspect of your plot.
  • Integration: Works seamlessly with NumPy arrays and Pandas DataFrames.
Basic Plotting

Your First Line Plot

The plt.plot() function is used to draw points (markers) in a diagram. By default, it draws a continuous line connecting the points.

xpoints = np.array([0, 6])
ypoints = np.array([0, 250])

# Draw the line
plt.plot(xpoints, ypoints)
# Display the plot
plt.show()
📍

Without x-points

If you only pass Y, Matplotlib assumes X is [0, 1, 2, 3...]

👁️

plt.show()

The crucial command that flushes the canvas and displays your final visual on screen.

Aesthetics

Customizing Markers

A "marker" emphasizes specific data points along your line. You specify it using the marker argument in plot().

ypoints = np.array([3, 8, 1, 10])

# 'o' = Circle marker  |  'D' = Diamond  |  '*' = Star
plt.plot(ypoints, marker='o', ms=20, mec='r', mfc='y')
plt.show()

Marker Sizes & Colors

  • ms (markersize) - sets the size.
  • mec (markeredgecolor) - border color.
  • mfc (markerfacecolor) - fill color.

Shorthand Notation: 'fmt'

You can format dynamically using: marker|line|color

Example: plt.plot(ypoints, 'o:r')
(Circle marker, dotted line, red color).

Aesthetics

Modifying the Line

You can change the style, width, and color of the line connecting your data points.

# Creating a thick, dashed, green line
plt.plot(
    ypoints,
    linestyle='dashed',  # or ls='--'
    color='#06d6a0',     # or c='g'
    linewidth=5.5        # or lw=5.5
)
Style NameShorthandDescription
'solid''-'Default connecting line
'dotted'':'Line mapped with dots
'dashed''--'Line separated by dashes
'dashdot''-.'Alternating dashes & dots
Metadata

Labels, Titles & Legends

A plot without labels is useless because the viewers don't know what the axes represent.

plt.plot(x, y, label='Sales Data')

# Set Title and Axis Labels
plt.title('Monthly Revenue', loc='left', fontsize=16)
plt.xlabel('Months')
plt.ylabel('Revenue (USD)')

# Show the legend based on the plot 'label' argument
plt.legend()

💡 Adding Fonts to Titles

You can use font dictionaries to style titles dynamically:
fontdict = {'family':'serif','color':'blue','size':20}
plt.title('Main', fontdict=fontdict)

Metadata

Adding a Grid

Grids make your charts dramatically easier to read by adding visual background guides.

# Enable default grid
plt.grid()

# Enable grid for just one axis
plt.grid(axis='x')

# Customize grid lines
plt.grid(color='green', linestyle='--', linewidth=0.5)

The grid() function takes the same styling parameters as lines, allowing you to establish beautiful, subtle guidelines.

Charts
Creating Different Types of Plots

Moving Beyond Line Graphs

Lines represent trends over time. Next, let's look at specialized plots to represent distributions, comparisons, and comparisons of proportions.

Chart Types

Scatter Plot

Use scatter() to observe relationships (correlations) between two metric variables. Each item in the array becomes a single dot.

x = np.array([5,7,8,7,2,17,2,9,4])
y = np.array([99,86,87,88,111,86,103,87,94])

plt.scatter(x, y)
plt.show()

Mapping Data to Colors & Sizes

To make scatter plots 4-Dimensional: Provide an array to c (Colors) and an array to s (Sizes).
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')

Chart Types

Bar Chart

Use bar() to compare categorical data (like cities, fruits, or years).

# Vertical Bars
x = np.array(["A", "B", "C"])
y = np.array([3, 8, 1])

plt.bar(x, y, width=0.1)
# Horizontal Bars
x = np.array(["A", "B", "C"])
y = np.array([3, 8, 1])

plt.barh(x, y, height=0.1)
  • For plt.bar() use width to compress the bars.
  • For plt.barh() use height to compress the bars.
  • You can color them dynamically by supplying a list of colors to the color parameter.
Chart Types

Histograms

A Histogram shows the frequency distribution of a given 1D array. It bins continuous data to see its spread.

# Generating 250 random values matching a normal distribution 
# centered at 170, with a std dev of 10
x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()

Histogram vs Bar Chart

Bar Charts compare distinct categories.
Histograms measure the distribution of a single continuous numeric variable into discrete 'bins'.

Chart Types

Pie Charts

Use pie() to show proportions of a whole.

y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0] # pop out the first slice

plt.pie(y, labels = mylabels, explode = myexplode, shadow = True)
plt.legend(title = "Fruits:")
plt.show()
  • explode: Array to offset a wedge from the center.
  • startangle: Change the rotation (default starts at the x-axis, degree 0).
  • shadow: Adds a beautiful 3D-like drop shadow.
Advanced Layouts

Working with Subplots

The subplot() function allows you to draw multiple plots in one single figure. It takes a 3-digit integer representing layout: (rows, columns, index)

# Plot 1: Will be drawn as 1st plot in a 1 Row, 2 Column grid 
plt.subplot(1, 2, 1)
plt.plot(x, y)

# Plot 2: Will be drawn as 2nd plot in a 1 Row, 2 Column grid 
plt.subplot(1, 2, 2)
plt.plot(x, y)

plt.show()

Adding a Super Title

Use plt.suptitle('My Main Dashboard') to add a title reflecting all the subplots combined.

Lab Tasks

Coding Exercises

Try these exercises to apply what you've learned. Write these in your Jupyter Notebook.

Exercise 1: The Fancy Line

Given `x=[1, 2, 3, 4]`, `y=[10, 20, 25, 30]`.
Plot a line graph. Make the line dashed, color it red, and add Diamond markers. Add X/Y labels and Title. Finally, display a grid.

Exercise 2: Comparison Bar Chart

Create two arrays: Languages `['Python', 'C++', 'Java']` and Popularity `[90, 50, 75]`.
Generate a Bar Chart. Color Python as 'green', C++ as 'red', and Java as 'blue'.

Exercise 3: Financial Dashboard

Create a 2x1 subplot grid (2 rows, 1 col).
Top cell: A Scatter plot showing Revenue vs Marketing Spend.
Bottom cell: A Histogram of Daily Transactions `np.random.normal(5000, 200, 100)`. Add a super title: "Q1 Performance".

Practice & Execution

Open the Python Lab Notebook

It is time to execute these snippets and visualize the data. Complete your experiments in the Lab's interactive environment.

Open in Google Colab
📓 Write Code ☁️ Visualize Output
Use arrow keys or swipe to navigate